package alexattar.gdxtest.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

import alexattar.gdxtest.DefaultActorListener;
import alexattar.gdxtest.FrontGun;
import alexattar.gdxtest.GdxTestGame;
import alexattar.gdxtest.Item;
import alexattar.gdxtest.Profile;
import alexattar.gdxtest.Shield;
import alexattar.gdxtest.Ship;
import alexattar.gdxtest.ShipModel;
import alexattar.gdxtest.services.SoundManager.TyrianSound;

public class StartGameScreen extends AbstractScreen {

	private Profile profile;
	private Ship ship;
	
	private TextButton episode1Button;
	private TextButton episode2Button;
	private TextButton episode3Button;
	private SelectBox shipModelSelectBox;
	private SelectBox frontGunSelectBox;
	private SelectBox shieldSelectBox;
	private Label creditsLabel;
	
	private Image shipModelImage;
	private Image frontGunImage;
	private Image shieldImage;
	
	private LevelClickListener levelClickListener;
	private ItemSelectionListener itemSelectionListener;
	
	public StartGameScreen(GdxTestGame game) {
		super(game);
		
		levelClickListener = new LevelClickListener();
		itemSelectionListener = new ItemSelectionListener();
	}
	
	@Override
	public void show() {
		super.show();
		
		Skin skin = super.getSkin();
		// retrieve the default table actor
        Table table = new Table(getSkin());
        table.setFillParent(true);
        stage.addActor(table);
        
        table.defaults().spaceBottom(20);
        table.columnDefaults(0).padRight(20);
        table.columnDefaults(4).padLeft(10);
        table.add("Start Game").colspan(5);
        
        profile = game.getProfileManager().retrieveProfile();
        ship = profile.getShip();
        
        table.row();
        table.add("Episodes");
        
        episode1Button = new TextButton("Episode 1", skin);
        episode1Button.addListener(levelClickListener);
        table.add(episode1Button).fillX().padRight(10);
        
        episode2Button = new TextButton("Episode 2", skin);
        episode2Button.addListener(levelClickListener);
        table.add(episode2Button).fillX().padRight(10);
        
        episode3Button = new TextButton("Episode 3", skin);
        episode3Button.addListener(levelClickListener);
        table.add(episode3Button).fillX().padRight(10);
        
        shipModelSelectBox = new SelectBox(ShipModel.values(), skin);
        shipModelSelectBox.addListener(itemSelectionListener);
        shipModelImage = new Image();
        
        table.row();
        table.add("Ship Model");
        table.add(shipModelSelectBox).fillX().colspan(3);
        table.add(shipModelImage);
        
        frontGunSelectBox = new SelectBox(FrontGun.values(), skin);
        frontGunSelectBox.addListener(itemSelectionListener);
        frontGunImage = new Image();
        
        table.row();
        table.add("Front Gun");
        table.add(frontGunSelectBox).fillX().colspan(3);
        table.add(frontGunImage);
        
        shieldSelectBox = new SelectBox(Shield.values(), skin);
        shieldSelectBox.addListener(itemSelectionListener);
        shieldImage = new Image();
        
        table.row();
        table.add("Shield Model");
        table.add(shieldSelectBox).fillX().colspan(3);
        table.add(shieldImage);
        
        creditsLabel = new Label("$" + profile.getCredits(), skin);
        table.row();
        table.add("Credits");
        table.add(creditsLabel).left().colspan(4);
        
        TextButton backButton = new TextButton("Back to main menu", skin);
        backButton.addListener(new DefaultActorListener() {
        	public void touchUp(
        			InputEvent event,
        			float x, float y,
        			int pointer, 
        			int button) {
				game.getSoundManager().play(TyrianSound.CLICK);
        		game.setScreen(new MenuScreen(game));
        	}
        });
        table.row();
        table.add(backButton).size(250,60).colspan(5);
        
        updateValues();
	}
	
	private void updateValues() {
		shipModelSelectBox.setSelection(ship.getShipModel().ordinal());
		frontGunSelectBox.setSelection(ship.getFrontGun().ordinal());
		shieldSelectBox.setSelection(ship.getShield().ordinal());
		
		String prefix = "start-game-screen/";
		TextureRegion shipModel = getAtlas().findRegion(
				prefix + ship.getShipModel().getSimpleName());
		TextureRegion frontGun = getAtlas().findRegion(
				prefix + ship.getFrontGun().getSimpleName());
		TextureRegion shield = getAtlas().findRegion(
				prefix + ship.getShield().getSimpleName());
		
		shipModelImage.setDrawable(new TextureRegionDrawable(shipModel));
		frontGunImage.setDrawable(new TextureRegionDrawable(frontGun));
		shieldImage.setDrawable(new TextureRegionDrawable(shield));
	}
	
	private class LevelClickListener extends DefaultActorListener {
		@Override
		public void touchUp(
				InputEvent event,
				float x, float y,
				int pointer,
				int button) {
			super.touchUp(event, x, y, pointer, button);
			game.getSoundManager().play(TyrianSound.CLICK);
			
			int targetLevelId = -1;
			Actor actor = event.getListenerActor();
			if (actor == episode1Button) {
				targetLevelId = 0;
			}
			else if (actor == episode2Button) {
				targetLevelId = 1;
			}
			else if (actor == episode3Button) {
				targetLevelId = 2;
			}
			else return;
			
			if (profile.getCurrentLevelId() >= targetLevelId) {
				Gdx.app.log(GdxTestGame.LOG, "Starting level: " + targetLevelId);
				game.setScreen(new LevelScreen(game, targetLevelId));
			}
			else {
				Gdx.app.log(GdxTestGame.LOG, "Unable to start level: " + targetLevelId);
			}
		}
	}
	
	private class ItemSelectionListener extends ChangeListener {
		@Override
		public void changed(
				ChangeEvent event,
				Actor actor) {
			Item selectedItem = null;
			int selectedIndex = ((SelectBox) actor).getSelectionIndex();
			if (actor == shipModelSelectBox) {
				selectedItem = ShipModel.values()[selectedIndex];
			}
			else if (actor == frontGunSelectBox) {
				selectedItem = FrontGun.values()[selectedIndex];
			}
			else if (actor == shieldSelectBox) {
				selectedItem = Shield.values()[selectedIndex];
			}
			else return;
			
			if (ship.contains(selectedItem)) return;
			
			if (profile.buy(selectedItem)) {
				creditsLabel.setText("$" + profile.getCredits());
			}
			
			updateValues();
		}
	}

}
